package org.deng.learn.designpattern.proxy;

import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @ Author: DengLibin
 * @ Description: 歌手的代理
 * @ DateTime:  15:44 2018/5/6 0006
 */
public class SingerProxy {
    private Singable singer =  new Singer();

    /**
     * 动态创建一个歌手的代理
     * @return
     */
    public Singable getSinger() {
        return (Singable) Proxy.newProxyInstance(SingerProxy.class.getClassLoader(),
                singer.getClass().getInterfaces(), (Object proxy, Method method, Object[] args)->{
                    String name = method.getName();
                    if(name.equals("sing"))
                    {
                        System.out.println("代理唱歌");
                        return method.invoke(singer,args);
                    }
                    return null;
                });
    }
}
